﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace Ymca.WebSecurity
{
	public class YmcaRoleProvider: RoleProvider
	{
		public override void AddUsersToRoles(string[] usernames, string[] roleNames)
		{
			using (DataContext dc = new DataContext())
			{
				foreach (string username in usernames)
				{
                    User user = dc.Users.Find(username);
                    if (user != null)
                    {
                        foreach (string roleName in roleNames)
                        {
                            Role role = dc.Roles.Find(roleName);
                            if (role != null)
                            {
                                UserToRole userToRole = dc.UsersToRoles.Where(x => x.UserName.Equals(username) && x.RoleName.Equals(roleName)).FirstOrDefault();
                                if (userToRole == null)
                                {
                                    dc.UsersToRoles.Add(new UserToRole()
                                    {
                                        UserName = username,
                                        RoleName = roleName
                                    });
                                }
                            }
                        }
                    }
				}
			}
		}

		public override string ApplicationName { get; set; }

		public override void CreateRole(string roleName)
		{
			using (DataContext dc = new DataContext())
			{
				Role role = new Role() { RoleName = roleName };

				if (dc.Roles.Find(role.RoleName) == null)
				{
					dc.Roles.Add(role);
				}
			}
		}

		public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
		{
			using (DataContext dc = new DataContext())
			{
				Role role = dc.Roles.Find(roleName);
				if (role != null)
				{
					dc.Roles.Remove(role);
					return true;
				}
				return false;
			}
		}

		public override string[] FindUsersInRole(string roleName, string usernameToMatch)
		{
			using (DataContext dc = new DataContext())
			{
                return dc.UsersToRoles.Where(x => x.RoleName.Equals(roleName) && x.UserName.Equals(usernameToMatch)).Select(x => x.UserName).ToArray();
			}
		}

		public override string[] GetAllRoles()
		{
            using (DataContext dc = new DataContext())
            {
                return dc.Roles.Select(x => x.RoleName).ToArray();
            }
		}

		public override string[] GetRolesForUser(string username)
		{
            using (DataContext dc = new DataContext())
            {
                return dc.UsersToRoles.Where(x => x.UserName.Equals(username)).Select(x => x.RoleName).ToArray();
            }
		}

		public override string[] GetUsersInRole(string roleName)
		{
            using (DataContext dc = new DataContext())
            {
                return dc.UsersToRoles.Where(x => x.RoleName.Equals(roleName)).Select(x => x.UserName).ToArray();
            }
		}

		public override bool IsUserInRole(string username, string roleName)
		{
            using (DataContext dc = new DataContext())
            {
                return dc.UsersToRoles.Where(x => x.UserName.Equals(username) && x.RoleName.Equals(roleName)).FirstOrDefault() != null ? true : false;
            }
		}

		public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
		{
            using (DataContext dc = new DataContext())
            {
                foreach (string username in usernames)
                {
                    foreach (string roleName in roleNames)
                    {
                        UserToRole userToRole = dc.UsersToRoles.Where(x => x.UserName.Equals(username) && x.RoleName.Equals(roleName)).FirstOrDefault();
                        if (userToRole != null)
                        {
                            dc.UsersToRoles.Remove(userToRole);
                        }
                    }
                }
            }
		}

		public override bool RoleExists(string roleName)
		{
            using (DataContext dc = new DataContext())
            {
                return dc.Roles.Find(roleName) != null ? true : false;
            }
		}
	}
}
